﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;



public partial class Admin_Experiment : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];
            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            fillDataGrid();
        }
    }
    private void fillDataGrid()
    {
        string ExperimentId = (string)Session["ExperimentId"];
        if (ExperimentId == null)
        {
            lblMessage.Text = "You haven't add experiment to this reference";
            trdgExperiment.Visible = false;
            trExperimenttxtBoxes.Visible = true;
        }
        else
        {
            Int32 ExpId = Convert.ToInt32(ExperimentId);
            if (objService.GetExperimentDetailsBasedonPK(ExpId).Tables[0].Rows.Count < 0)
            {

                trdgExperiment.Visible = false;
                trExperimenttxtBoxes.Visible = true;
            }
            else
            {
                dgExperiment.DataSource = objService.GetExperimentDetailsBasedonPK(ExpId);
                 dgExperiment.DataBind();
                trdgExperiment.Visible = true;
                trExperimenttxtBoxes.Visible = false;
            }
        }
    }

     
    protected void dgExperiment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       dgExperiment.PageIndex = e.NewPageIndex;
       fillDataGrid();
    }

    protected void dgExperiment_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgExperiment.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillExperimentDetails(id);
        trdgExperiment.Visible = false;
        trExperimenttxtBoxes.Visible = true;
    }

    private void fillExperimentDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetExperimentDetailsBasedonPK(id);
        txtSummary.Text = ds.Tables[0].Rows[0]["exp_summary"].ToString();
        txtExpId.Text = ds.Tables[0].Rows[0]["experiment_id"].ToString();
        txtExperimentName.Text = ds.Tables[0].Rows[0]["ExperimentName"].ToString();
    }

    private void clearTextBoxes()
    {
        txtExpId.Text = string.Empty;
        txtSummary.Text = string.Empty;
        txtExperimentName.Text = string.Empty;
    }
    
    protected void dgExperiment_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    protected void dgExperiment_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnSave_Click1(object sender, ImageClickEventArgs e)
    {
        if (txtExpId.Text != string.Empty)
        {
            if (txtSummary.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateExperiment("exp_summary", "null", txtExpId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateExperiment("exp_summary", txtSummary.Text, txtExpId.Text);
            }
            if (txtExperimentName.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateExperiment("ExperimentName", "null", txtExpId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateExperiment("ExperimentName", txtExperimentName.Text, txtExpId.Text);
            }
            fillDataGrid();
            clearTextBoxes();
        }
        else
        {
            string ReferenceID = (string)Session["ReferenceId"];

            if ((string)Session["ExperimentId"] != null)
            {
                string ExpId = (string)Session["ExperimentId"];
                lblMessage.Text = objService.UpdateExperiment("exp_summary", txtSummary.Text, ExpId);
                lblMessage.Text = objService.UpdateExperiment("ExperimentName", txtExperimentName.Text, ExpId);
                lblMessage.Text = objService.UpdateExperiment("ref_fk", ReferenceID, ExpId);
                fillDataGrid();
                clearTextBoxes();
            }
            else
            {

                lblMessage.Text = objService.SaveExperiment(txtSummary.Text, null, null, null, null, null, null, null, null, null, null, ReferenceID, txtExperimentName.Text);
                Int32 ExperimentId = Convert.ToInt32(objService.GetLastInsertedId("ref_fk", ReferenceID, "experiment"));
                Session["ExperimentId"] = ExperimentId.ToString();
                fillDataGrid();
                clearTextBoxes();
            }
        }
    }
    protected void dgExperiment_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        dgExperiment.PageIndex = e.NewPageIndex;
        fillDataGrid();
    }
}
